using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Meteorite
{
    public class StoneManage
    {
        public enum ObjType
        {
            eObjType_SpeedUp = 0,
            eObjType_Fuel,
            eObjType_Bounus,

            eObjType_Stone1 = 10,
            eObjType_Stone2,
            eObjType_Stone3,
            eObjType_Stone4,

            eObjType_Max,
        };
        Random rand = new Random();

        List<Object> listEntity = new List<Object>();

        public int Level { get; set; }
        int nTick = 0;
        int nSpecialItemCountDown = 0;
        int nNextTowardShipCountDown = 0;

        Model SpeedupModel;
        Model FuelModel;
        Model BounusModel;

        Model StoneModel1;
        Model StoneModel2;
        Model StoneModel3;
        Model StoneModel4;
        public StoneManage()
        { 
            Level = 1;
            nTick = 0;
            nSpecialItemCountDown = rand.Next();
            nNextTowardShipCountDown = rand.Next();
            //nStoneCountDown = 
        }

        public void LoadContent(Meteorite game)
        {
            SpeedupModel = game.Content.Load<Model>(@"meshs/a");
            FuelModel = game.Content.Load<Model>(@"meshs/a");
            BounusModel = game.Content.Load<Model>(@"meshs/a");

            StoneModel1 = game.Content.Load<Model>(@"meshs/a");
            StoneModel2 = StoneModel1;
            StoneModel3 = StoneModel1;
            StoneModel4 = StoneModel1;

            /*
            StoneModel1 = game.Content.Load<Model>(GameConstant.Model_Stone_1);
            StoneModel2 = game.Content.Load<Model>(GameConstant.Model_Stone_2);
            StoneModel3 = game.Content.Load<Model>(GameConstant.Model_Stone_3);
            StoneModel4 = game.Content.Load<Model>(GameConstant.Model_Stone_4);
            */
            Vector3 p = Meteorite.camera.Position;

            for (int i = 0; i < 10; i++ )
            {
                ObjType Objtype = (ObjType)rand.Next((int)(ObjType.eObjType_Stone1), (int)(ObjType.eObjType_Stone4));
                GenerateObj(Objtype, false, false);
            }
        }

        public void Draw(Matrix View, Matrix Projection)
        {
            foreach (Object e in listEntity)
            {
                e.Draw(View, Projection);
            }
        }

        public List<Object> GetEntityList()
        {
            return listEntity;
        }

        public void GenerateObj(ObjType type,bool bFarthest,bool bTowardShip)
        {
            Object Obj = null;
            switch (type)
            {
                case ObjType.eObjType_SpeedUp:
                    {
                        Obj = new Speedup(SpeedupModel);
                        UserData.Instance().AddValue(GameConstant.UserData_TotalBounus, 1);
                    }
                    break;
                case ObjType.eObjType_Fuel:
                    {
                        Obj = new Fuel(FuelModel);
                        UserData.Instance().AddValue(GameConstant.UserData_TotalBounus, 1);
                    }
                    break;
                case ObjType.eObjType_Bounus:
                    {
                        Obj = new Bounus(BounusModel);
                        UserData.Instance().AddValue(GameConstant.UserData_TotalBounus, 1);
                    }
                    break;
                case ObjType.eObjType_Stone1:
                    {
                        Obj = new Stone(StoneModel1);
                        Obj.Scale = 1.1f + 1.1f * (float)Meteorite.random.NextDouble();
                        UserData.Instance().AddValue(GameConstant.UserData_TotalStone, 1);
                    }
                    break;
                case ObjType.eObjType_Stone2:
                    {
                        Obj = new Stone(StoneModel2);
                        Obj.Scale = 4.0f + 4.0f * (float)Meteorite.random.NextDouble();
                        UserData.Instance().AddValue(GameConstant.UserData_TotalStone, 1);
                    }
                    break;
                case ObjType.eObjType_Stone3:
                    {
                        Obj = new Stone(StoneModel3);
                        Obj.Scale = 4.0f + 4.0f * (float)Meteorite.random.NextDouble();
                        UserData.Instance().AddValue(GameConstant.UserData_TotalStone, 1);
                    }
                    break;
                case ObjType.eObjType_Stone4:
                    {
                        Obj = new Stone(StoneModel4);
                        Obj.Scale = 4.0f + 4.0f * (float)Meteorite.random.NextDouble();
                        UserData.Instance().AddValue(GameConstant.UserData_TotalStone, 1);
                    }
                    break;
                default:
                    return;
            }

            Vector3 Pos = Vector3.Zero;
            if (bTowardShip)
            {
                Pos.X = Meteorite.ship.Position.X;
                Pos.Y = Meteorite.ship.Position.X;
            }
            else
            {
                Pos.X = Meteorite.random.Next(-600, 600);
                Pos.Y = Meteorite.random.Next(-600, 600);
            }

            if (bFarthest)
            {
                Pos.Z = Meteorite.camera.Position.Z - 10000;
            }
            else
            {
                Pos.Z = Meteorite.random.Next(-10000, 0);
            }
            Obj.Position = Pos;
            Obj.Velocity = new Vector3(0, 0, Meteorite.random.Next(1000, 4000) * 1.0f);
            Obj.Rotation = new Vector3(6.0f * (float)Meteorite.random.NextDouble(), 6.0f * (float)Meteorite.random.NextDouble(), 6.0f * (float)Meteorite.random.NextDouble());
            Obj.Valid = true;

            listEntity.Add(Obj);
        }

        public void Update(GameTime gameTime)
        {
            nTick++;
            ObjType Objtype = (ObjType)rand.Next((int)(ObjType.eObjType_Stone1), (int)(ObjType.eObjType_Stone4));

            if (nSpecialItemCountDown > 0)
            {
                nSpecialItemCountDown--;
            }
            if (nNextTowardShipCountDown > 0)
            {
                nNextTowardShipCountDown--;
            }

            if( 0==nTick%5 )
            {
                GenerateObj(Objtype, true, false);
            }

            List<Object> list = new List<Object>();
            //Recycle
            foreach (Object o in listEntity)
            {
                if( o.Valid )
                {
                    if ((o.Position.Z - Meteorite.camera.Position.Z) > 1000)
                    {
                        list.Add(o);
                    }
                    else
                    {
                        o.Update(gameTime);
                    }
                }
            }

            //Record user data
            for (int i = 0; i < list.Count; i++)
            {
                listEntity.Remove(list[i]);
                GameScene.nScore += 10;
            }

        }
    }
}
